Please ask about problems and questions regarding this tutorial on answers.ros.org. Don't forget to include in your question the link to this page, the versions of your OS & ROS, and also add appropriate tags. |
Python tutorial to interact with open_loop_object_manipulation_actionserver
Description: This tutorial will show you how to interact with the open_loop_object_manipulation_actionserver in PythonTutorial Level: INTERMEDIATE
Contents
Introduction
Writing The Code
This piece shows the whole code. Then below we'll slowly break things down bit by bit.
1 import roslib; roslib.load_manifest('aaai_lfd_demo_executive')
2 import rospy
3 import actionlib
4 import math
5 import copy
6
7 import openloop_object_manipulation.msg as manipulation_action_msg
8
9 from pr2_pick_and_place_demos.pick_and_place_manager import *
10 from object_manipulator.convert_functions import *
11
12 class AAAILfDObjectGrasper:
13
14 def __init__(self):
15
16 self.manipulation_server = actionlib.SimpleActionClient('open_loop_object_manipulation_actionserver', manipulation_action_msg.OpenLoopObjectManipulationAction)
17 rospy.loginfo('Waiting for manipulation server')
18 self.manipulation_server.wait_for_server()
19
20
21 def manipulate(self, object, action, arm):
22 print "manipulating object: %s action: %s arm %s", (object, action, arm)
23 goal = manipulation_action_msg.OpenLoopObjectManipulationGoal()
24 goal.object = object
25 goal.action = action
26 goal.arm = arm
27
28 rospy.loginfo('Sending goal')
29 self.manipulation_server.send_goal(goal)
30 rospy.loginfo('Waiting for results')
31 self.manipulation_server.wait_for_result()
32
33
34 if __name__ == '__main__':
35 try:
36 # Initializes a rospy node so that the SimpleActionClient can
37 # publish and subscribe over ROS.
38 rospy.init_node('aaai_lfd_demo_manipulator')
39 manipulation = AAAILfDObjectGrasper()
40 manipulation.manipulate("tea-box", "pick", "l")
41
42 except rospy.ROSInterruptException:
43 print "program interrupted before completion"
In this example, we create a class to manipulate objects of pre-recorded positions. In particular this code will result in the tea-box being picked up using the left arm.
== Initialization ==
1 import roslib; roslib.load_manifest('aaai_lfd_demo_executive')
2 import rospy
3 import actionlib
4 import math
5 import copy
6
7 import openloop_object_manipulation.msg as manipulation_action_msg
8
9 from pr2_pick_and_place_demos.pick_and_place_manager import *
10 from object_manipulator.convert_functions import *
This portion of the code includes the files necessary to write the code
Class Initialization
1 class AAAILfDObjectGrasper:
2
3 def __init__(self):
4
5 self.manipulation_server = actionlib.SimpleActionClient('open_loop_object_manipulation_actionserver', manipulation_action_msg.OpenLoopObjectManipulationAction)
6 rospy.loginfo('Waiting for manipulation server')
7 self.manipulation_server.wait_for_server()
This declares a class AAAILfDObjectGrasper and the init function. Within the init function we can break things down further:
self.manipulation_server = actionlib.SimpleActionClient('open_loop_object_manipulation_actionserver', manipulation_action_msg.OpenLoopObjectManipulationAction)
creates self.manipulation_server an action client to the open_loop_object_manipulation_actionserver.
rospy.loginfo('Waiting for manipulation server') self.manipulation_server.wait_for_server()
These lines wait for the action server to respond that it is running.
Using the Action Server
1 def manipulate(self, object, action, arm):
2 print "manipulating object: %s action: %s arm %s", (object, action, arm)
3 goal = manipulation_action_msg.OpenLoopObjectManipulationGoal()
4 goal.object = object
5 goal.action = action
6 goal.arm = arm
7
8 rospy.loginfo('Sending goal')
9 self.manipulation_server.send_goal(goal)
10 rospy.loginfo('Waiting for results')
11 self.manipulation_server.wait_for_result()
def manipulate(self, object, action, arm):
Defines a function manipulate that takes three arguments: the desired object, the action, and which arm should be used
goal = manipulation_action_msg.OpenLoopObjectManipulationGoal() goal.object = object goal.action = action goal.arm = arm
These lines create a goal for the open_loop_object_manipulation_actionserver
self.manipulation_server.send_goal(goal)
This line sends the goal to the action server
self.manipulation_server.wait_for_result()
This line waits for the result
Picking up the Tea-Box
This code shows the main function which uses the class we created in the previous sections.
1 if __name__ == '__main__':
2 try:
3 # Initializes a rospy node so that the SimpleActionClient can
4 # publish and subscribe over ROS.
5 rospy.init_node('aaai_lfd_demo_manipulator')
6 manipulation = AAAILfDExecutive()
7 manipulation.manipulate("tea-box", "pick", "l")
8
9 except rospy.ROSInterruptException:
10 print "program interrupted before completion"
rospy.init_node('aaai_lfd_demo_manipulator')
Initializes this rosnode
manipulation = AAAILfDExecutive()
Creates a new instance of the class we created
manipulation.manipulate("tea-box", "pick", "l")
Asks the class to pickup the teabox with the left arm.